import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import sun.misc.IOUtils;

import java.io.IOException;
import java.io.InputStream;

@FunctionalInterface
public interface RequestHandler {
     FullHttpResponse handle(FullHttpRequest request);

      default FullHttpResponse getTextResponse(String pageMsg){
         // 创建http响应
         FullHttpResponse response = new DefaultFullHttpResponse(
                 HttpVersion.HTTP_1_1,
                 HttpResponseStatus.OK,
                 Unpooled.copiedBuffer(pageMsg, CharsetUtil.UTF_8));
         // 设置头信息
         response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
         return  response;
     }


    default FullHttpResponse getIcon(InputStream icon)  {
        // 创建http响应
        FullHttpResponse response = null;
        try {
            response = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    Unpooled.copiedBuffer(IOUtils.readFully(icon,icon.available(),false)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 设置头信息
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "image/x-icon");
        return  response;
    }
}
